﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Commonlib;
using System.Data.SqlClient;
using System.Data;

namespace AlbonWeb.Manager.OrderManager
{
    public partial class PayMent : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["limit"] = "where totalMoney>0";
                bindInvoice();    
            }
        }

        protected void bindInvoice()
        {
            //查询条件
            if (refNo.Text.Trim() != "")
            {
                ViewState["limit"] += " and refNo like '%"+refNo.Text.Trim()+"%'";
            }
            if (userName.Text.Trim()!="")
            {
                ViewState["limit"] += " and userName like '%"+userName.Text.Trim()+"%'";
            }
            //绑定数据
            string cmdstr = "select count(*) from InvoiceView " + ViewState["limit"].ToString();
            AspNetPager1.RecordCount = int.Parse(SqlHelper.ExecuteScalar(SqlHelper.SqlConnectionString, CommandType.Text, cmdstr, null).ToString());
            SqlParameter[] parameters = new SqlParameter[]{
                new SqlParameter("@tablename","InvoiceView"),
                new SqlParameter("@startIndex",AspNetPager1.StartRecordIndex),
                new  SqlParameter("@endIndex",AspNetPager1.EndRecordIndex),
                new SqlParameter("@limit",ViewState["limit"].ToString()),
                new SqlParameter("@orderby","totalDate desc")
            };
            SqlHelper.BindRepeater(repMain, SqlHelper.SqlConnectionString, CommandType.StoredProcedure, "netpager", parameters);
            //总数统计
            cmdstr = "select * from InvoiceView "+ViewState["limit"].ToString();
            DataTable dt = SqlHelper.ExecuteDataSet(SqlHelper.SqlConnectionString, CommandType.Text, cmdstr, null).Tables[0];
            decimal total = 0;
            decimal balance = 0;
            foreach (DataRow row in dt.Rows)
            {
                total += decimal.Parse(row["totalMoney"].ToString());
                BLL.PayMentBLL paymentbll=new BLL.PayMentBLL();
                balance += paymentbll.Balance(row["invoiceNo"].ToString());
            }
            finalTotal.Text = total.ToString("0.00");
            Balance.Text = balance.ToString("0.00");
        }
        //查询
        protected void btnSearch_Click(object sender, ImageClickEventArgs e)
        {
            ViewState["limit"] = "where totalMoney>0";
            bindInvoice();
        }

        //计算欠款
        protected void repMain_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemIndex != -1)
            {
                string invoiceNo=((Label)e.Item.FindControl("invoiceNo")).Text;
                Image Status = (Image)e.Item.FindControl("Status");
                Label Balance = (Label)e.Item.FindControl("Balance");
                BLL.PayMentBLL paymentbll = new BLL.PayMentBLL();
                decimal balance=paymentbll.Balance(invoiceNo);
                Balance.Text = balance.ToString("0.00") ;
                if (balance < 0)
                {
                    Balance.ForeColor = System.Drawing.Color.Red;
                }
                else
                {
                    Status.ImageUrl = "/images/finished_bill.gif";
                }
            }
        }
        //换页
        protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            AspNetPager1.CurrentPageIndex = e.NewPageIndex;
            bindInvoice();
        }

        
    }
}